package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderGoodsMapper;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsOrderService;
import com.qf.vo.DayStatis;
import com.qf.vo.StatOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DtsOrderServiceImpl implements DtsOrderService {

    @Autowired
    DtsOrderMapper orderMapper;

    @Autowired
    DtsOrderGoodsMapper orderGoodsMapper;

    @Autowired
    DtsUserMapper userMapper;

    @Autowired
    StatMapper statMapper;

    @Override
    public long getTotal() {
        return orderMapper.selectCount(null);
    }

    @Override
    public List<DayStatis> findOrderAmts() {
        return statMapper.findOrderAmts();
    }

    @Override
    public IPage<DtsOrder> queryPage(Integer page, Integer limit, String sort, String order, Integer[] orderStatusArray, Integer userId, Integer orderSn) {
        IPage<DtsOrder> iPage = new Page<>(page,limit);
        QueryWrapper<DtsOrder> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(userId)){
            qw.like("`user_id`",userId);
        }
        if(!StringUtils.isEmpty(orderSn)){
            qw.like("order_sn",orderSn);
        }
        if(!StringUtils.isEmpty(orderStatusArray)){
            for (Integer integer : orderStatusArray) {
                qw.in("order_status",integer);
            }
        }
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        orderMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public List<Map<String, Object>> queryShipChannel() {

        List<Map<String, Object>> collect = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("value","HTKY");
        map1.put("label","百世快递");
        collect.add(map1);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("value","ZTO");
        map2.put("label","中通快递");
        collect.add(map2);
        Map<String,Object> map3 = new HashMap<>();
        map3.put("value","YTO");
        map3.put("label","圆通速递");
        collect.add(map3);
        Map<String,Object> map4 = new HashMap<>();
        map4.put("value","YD");
        map4.put("label","韵达速递");
        collect.add(map4);
        return collect;
    }

    @Override
    public List<StatOrderVo> queryStatOrder() {
        return statMapper.queryStatOrder();
    }

    @Override
    public Map<String, Object> queryDetail(Integer id) {
        DtsOrder order = orderMapper.selectById(id);
        DtsUser user = userMapper.selectById(order.getUserId());
        LambdaQueryWrapper<DtsOrderGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsOrderGoods::getOrderId,order.getId());
        List<DtsOrderGoods> dtsOrderGoods = orderGoodsMapper.selectList(qw);

        Map<String,Object> map = new HashMap<>();
        map.put("orderGoods",dtsOrderGoods);
        map.put("user",user);
        map.put("order",order);
        return map;
    }

    @Override
    public void update(Integer orderId, String shipChannel, String shipSn) {
        DtsOrder order = new DtsOrder();
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);
        order.setUpdateTime(new Date());
        order.setOrderStatus(301);
        LambdaQueryWrapper<DtsOrder> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsOrder::getId,orderId);
        orderMapper.update(order,qw);
    }

}
